package cn.liuyi.weiyi.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import cn.edu.pku.tools.BitmapHelper;
import cn.edu.pku.tools.RegistrationInfo;
import cn.edu.pku.tools.TransactionRecord;
import cn.edu.pku.tools.YJRApplication;
import cn.liuyi.weiyi.R;
import cn.yjr.fragment.ChatRoomFragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author vector
 *
 */
public class ChatRoomAdapter extends BaseAdapter{

	Context context;
	LayoutInflater inflater;
	JSONArray doctors;
	JSONObject doctor;
	String consultStatus = null;
	Long deadline = (long) 0;
	boolean sendable = false;
	String id = null;
	String name =null;
	String hospital = null;
	String head = null;
	String category = null;
	String section = null;
	String title = null;
	String title_name = null;
	boolean recommend = false;
	String groupIntro;
	String notice, skills, type;
	ImageLoader imageLoader;
	DisplayImageOptions options;
	public ChatRoomAdapter(Context context,ImageLoader imageLoader, DisplayImageOptions options) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.imageLoader = imageLoader;
		this.options = options;
		doctors = ChatRoomFragment.doctors;
		
	}

	@Override
	public int getCount() {
		
		//return doctors.length();
		return doctors.length();
	}

	@Override
	public Object getItem(int position) {
		try {
			return doctors.getJSONObject(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.doctor_item, parent,false);
			holder.doctor_name_tv = (TextView) convertView.findViewById(R.id.doctor_name_tv);
			holder.work_info_tv = (TextView) convertView.findViewById(R.id.work_info_tv);
			holder.zc_info_tv= (TextView) convertView.findViewById(R.id.zc_info_tv);
			holder.zc_name_tv= (TextView) convertView.findViewById(R.id.zc_name_tv);
			holder.left_count_tv= (TextView) convertView.findViewById(R.id.left_count_tv);
			holder.time_tv= (TextView) convertView.findViewById(R.id.time_tv);
			holder.last_msg_tv= (TextView) convertView.findViewById(R.id.last_msg_tv);
			holder.doctor_head_imv = (ImageView) convertView.findViewById(R.id.doctor_head_imv);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			
			JSONObject doctor_ = doctors.getJSONObject(position);
			consultStatus = doctor_.getString("consultStatus");
			deadline = doctor_.getLong("deadline");
			doctor = doctor_.getJSONObject("doctor");
			
			sendable = doctor_.getBoolean("sendable");
			name = doctor.getString("name");
			hospital = doctor.getJSONObject("hospital").getString("text");
			head = doctor.getString("avatar");
			category = doctor.getString("category");
			recommend = doctor.getBoolean("recommend");
			section = doctor.getJSONObject("section").getString("text");
			groupIntro = doctor.getString("groupIntro");
			title = doctor.getString("title");
			notice = doctor.getString("notice");
			skills = doctor.getString("skills");
			type = doctor.getString("type");
			id = doctor.getString("id");
			title_name = YJRApplication.initData.getJSONObject("response").getJSONObject("codings").getJSONObject("doctorCategory").getJSONObject(category).getJSONObject("title").getString(title);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		holder.doctor_name_tv.setText(name + " 医生");
		holder.work_info_tv.setText(hospital + " " + section + " " +title_name);
		holder.zc_name_tv.setText("众筹项目名称");
		holder.zc_info_tv.setText("￥10,100已筹/52赞");
		holder.time_tv.setText("11:20");
		if(consultStatus.equals("N")){
			holder.last_msg_tv.setText("[未开始咨询]");
		}else if(consultStatus.equals("F")){
			holder.last_msg_tv.setText("[已结束]");
		}else if(consultStatus.equals("C")){
			holder.last_msg_tv.setText("[正在咨询]");
		}else if(consultStatus.equals("A")){
			holder.last_msg_tv.setText("[申请咨询中 剩余24小时00分钟]");
			holder.last_msg_tv.setTextColor(context.getResources().getColor(R.color.red_font));
		}else{
			holder.last_msg_tv.setText("[等待支付]");
		}
		
		//holder.doctor_head_imv.setImageBitmap(BitmapHelper.toRoundBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.head)));
		imageLoader.displayImage(YJRApplication.head_path+head, holder.doctor_head_imv, options);

		return convertView;
	}

    class ViewHolder {
        RelativeLayout doctor_info_rl,crowd_program_rl,chat_rl;
        ImageView doctor_head_imv;
        TextView doctor_name_tv,work_info_tv,zc_info_tv,zc_name_tv,left_count_tv,time_tv,last_msg_tv;
    }
	
}
